// Copyright 2011 Jefferson Willian
// willmont7@gmail.com

#include <cstdio>

int main() {
  bool win[100];
  int command;
  int queue[100];
  int round;
  int n, p;
  int temp;
  int games = 1;

  while (scanf("%d%d", &n, &p) && n != 0 || p != 0) {
    for (int i = 0;i < n;i++) {
      scanf("%d", &temp);
      queue[i] = temp;
      win[i] = true;
    }

    for (int i = 0;i < p;i++) {
      scanf("%d%d", &round, &command);

      int k = 0;

      for (int j = 0;j < round;j++) {
        /* Verifica o primeiro da fila original que ainda esta no jogo */
        while (!win[k]) k++;
        scanf("%d", &temp);
        /* Define o novo estado de vitoria */
        win[k++] = (temp == command);
      }
    }

    for (int i = 0;i < n;i++)
      if (win[i]) {
        printf("Teste %d\n%d\n\n", games++, queue[i]);
        break;
      }
  }

  return 0;
}
